export interface makePageInit { section: string; gyazo: string; text: string; /** ページ番号 */ pageNum: number; prevSection?: string; nextSection?: string; } export const makePage = (init: makePageInit): { title: string; lines: string[]; } => { const { section, gyazo, text, pageNum, prevSection, nextSection, } = init; const title = `${format(section)} ${pageNum}`; const prevTitle = prevSection ? `${format(prevSection)} ${pageNum - 1}` : undefined; const nextTitle = nextSection ? `${format(nextSection)} ${pageNum + 1}` : undefined; return { title, lines: [ title, ...format(text).split("\n"), "", `<=${ prevTitle ? `[${prevTitle}]` : "" }|${ nextTitle ? `[${nextTitle}]` : "" }=>`, "", `[${gyazo}]`, `[${section}]`, ], }; } const format = (text: string): string => text .replace(/\s+$/, "") // 末尾の余計な空白を消す .replace(/^(\s*)・/, "$1 ") // ・を箇条書きに変える .replace( /[A-Za-z0-9]/g, (s) => String.fromCharCode(s.charCodeAt(0) - 0xFEE0), ) // 全角英数を半角に直す .replace(/\s?\[/g, "[") // リンク記法をescapeする .replace(/\s?\[/g, "[") .replace(/(\d+).\s*/g, "$1. ") // 番号付き箇条書きにする .replaceAll(".", "。") // 句読点を変換する .replaceAll(",", "、");